﻿using System;
using System.Collections.Generic;
using System.Text;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools;

namespace BasecampSync
{
    public partial class PluginMain
    {
        #region Outlook buttons

        private Office.CommandBarButton menuTaskSync;
        private Office.CommandBarButton menuAbout;
        private Office.CommandBarButton menuSettings;
        private Office.CommandBarButton menuMilestoneSync;

        #endregion

        private BasecampApi api;
        private List<Project> myBasecampProjects;


        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            if (Config.Load())
            {
                this.api = new BasecampApi(Config.BasecampUsername, Config.BasecampPassword);
                myBasecampProjects = api.GetProjectList();
            }

            AddMenubar();
        }

        private void DisableButtons()
        {
            this.menuTaskSync.Enabled = false;
            this.menuSettings.Enabled = false;
            this.menuMilestoneSync.Enabled = false;
        }

        private void AddMenubar()
        {
            try
            {
                Office.CommandBarPopup cmdBarControl = null;

                Office.CommandBar menubar = this.Application.ActiveExplorer().CommandBars.ActiveMenuBar;
                
                int controlCount = menubar.Controls.Count;
                
                cmdBarControl = (Office.CommandBarPopup)menubar.Controls.Add(Office.MsoControlType.msoControlPopup, missing, missing, controlCount, true);

                if (cmdBarControl != null)
                {
                    cmdBarControl.Caption = "Basecamp";
                    cmdBarControl.Tag = "Basecamp";

                    // Menu point "Sync Tasks"
                    menuTaskSync = (Office.CommandBarButton)cmdBarControl.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, true);
                    menuTaskSync.Caption = "Sync Tasks";
                    menuTaskSync.Tag = "Sync Tasks";
                    menuTaskSync.FaceId = 0060; //65;
                    menuTaskSync.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(menuSyncTask_click);

                    // Menu point "Sync Milestones"
                    menuMilestoneSync = (Office.CommandBarButton)cmdBarControl.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, true);
                    menuMilestoneSync.Caption = "Sync Milestones";
                    menuMilestoneSync.Tag = "Sync Milestones";
                    menuMilestoneSync.FaceId = 0060; //65;
                    menuMilestoneSync.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(menuMilestoneSync_click);

                    // Menu point "Settings"
                    menuSettings = (Office.CommandBarButton)cmdBarControl.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, true);
                    menuSettings.Caption = "Settings";
                    menuSettings.Tag = "Setting";
                    menuSettings.FaceId = 0060; //65;
                    menuSettings.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(menuSettings_click);

                    // Menu point "About"
                    menuAbout = (Office.CommandBarButton)cmdBarControl.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, true);
                    menuAbout.Caption = "About";
                    menuAbout.Tag = "About";
                    menuAbout.FaceId = 0060; //65;
                    menuAbout.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(menuAbout_click);
                }
            }
            catch
            {
              
            }
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        public void menuSyncTask_click(Office.CommandBarButton cmdBarbutton, ref bool cancel)
        {
            if (Config.Load())
            {
                Outlook._Application outlookObj = new Outlook.Application();
                Outlook.Explorer explorer = this.Application.ActiveExplorer();

                TaskSync test = new TaskSync(outlookObj, this.api);
                test.Sync(myBasecampProjects);
            }
            else
            {
                // No configuration settings present
                FrmSettings settings = new FrmSettings();
                settings.ShowDialog();
            }
        }

        public void menuMilestoneSync_click(Office.CommandBarButton cmdBarbutton, ref bool cancel)
        {
            if (Config.Load())
            {
                Outlook._Application outlookObj = new Outlook.Application();
                Outlook.Explorer explorer = this.Application.ActiveExplorer();

                MilestoneSync mSync = new MilestoneSync(outlookObj, this.api);
                mSync.Sync(myBasecampProjects);
            }
            else
            {
                // No configuration settings present
                FrmSettings settings = new FrmSettings();
                settings.ShowDialog();
            }
        }

        public void menuSettings_click(Office.CommandBarButton cmdBarbutton, ref bool cancel)
        {
            FrmSettings settings = new FrmSettings();
            settings.ShowDialog();
        }

        public void menuAbout_click(Office.CommandBarButton cmdBarbutton, ref bool cancel)
        {
            FrmAbout about = new FrmAbout();
            about.ShowDialog();
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }
}
